package com.wowudi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wowudi.dto.Result;
import com.wowudi.entity.Menu;
import com.wowudi.entity.Meta;
import com.wowudi.entity.RoleMenu;
import com.wowudi.mapper.RoleMenuMapper;
import com.wowudi.service.MenuService;
import com.wowudi.service.MetaService;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wowudi.entity.Role;
import com.wowudi.mapper.RoleMapper;
import com.wowudi.service.RoleService;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private MenuService menuService;

    @Autowired
    private MetaService metaService;

    /**
    * Description: 查询所有角色<br/>
    * date: 2022-3-17<br/>
    * @author 贺同志<br/>
    */
    @Override
    public Result findAll() {
        List<Role> roleList = roleMapper.selectList(null);
        Result result = new Result();
        result.setData(roleList);
        return result;
    }


    /**
    * Description: 查询所有角色带分页带条件<br/>
    * date: 2022-3-23<br/>
    * @author 贺同志<br/>
    */
    @Override
    public Page<Role> findPageByName(Integer currentPage,Integer limit,String name) {
        Page<Role> page = new Page<>(currentPage,limit);
        Page<Role> rolePage = roleMapper.selectPage(page, new QueryWrapper<Role>().like("role_name", name));
        return rolePage;
    }


    /**
    * Description: 添加角色信息<br/>
    * date: 2022-3-23<br/>
    * @author 贺同志<br/>
    */
    @Override
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    public int addRole(Role role,Integer[] menuIds) {
        int i = roleMapper.insert(role);
        for (int j = 0; j < menuIds.length; j++) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role.getId());
            roleMenu.setMenuId(menuIds[j]);
            roleMenuMapper.insert(roleMenu);

            Menu menu = menuService.getMenuById(menuIds[j]);
            Meta meta = new Meta();
            meta.setMetaId(menu.getMetaId());
            meta.setRoles(menu.getMeta().getRoles()+","+role.getRoleName());
            metaService.updateById(meta);
        }
        return i;
    }


    /**
    * Description: 修改角色信息<br/>
    * date: 2022-3-25<br/>
    * @author 贺同志<br/>
    */
    @Override
    public int updateRole(Role role, Integer[] menuIds) {
        int i = roleMapper.updateById(role);
        roleMenuMapper.delete(new QueryWrapper<RoleMenu>().eq("role_id", role.getId()));
        List<Menu> menuList = menuService.list();
        menuList.forEach(item -> {
            Menu menu = menuService.getMenuById(item.getId());
            String[] split = menu.getMeta().getRoles().split(",");
            if (Arrays.asList(menuIds).contains(item.getId())){
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(role.getId());
                roleMenu.setMenuId(item.getId());
                roleMenuMapper.insert(roleMenu);

                if (!Arrays.asList(split).contains(role.getRoleName())){
                    Meta meta = new Meta();
                    meta.setMetaId(menu.getMetaId());
                    meta.setRoles(menu.getMeta().getRoles()+","+role.getRoleName());
                    metaService.updateById(meta);
                }
            }else{
                if (Arrays.asList(split).contains(role.getRoleName())){
                    ArrayList<String> list = new ArrayList<>(Arrays.asList(split));
                    list.remove(role.getRoleName());
                    Meta meta = new Meta();
                    meta.setMetaId(menu.getMetaId());
                    meta.setRoles(StringUtils.join(list.toArray(),","));
                    metaService.updateById(meta);
                }
            }
        });
        return i;
    }


    /**
    * Description: 根据id删除信息<br/>
    * date: 2022-3-25<br/>
    * @author 贺同志<br/>
    */
    @Override
    public int deleteRole(Integer id) {
        Role role = roleMapper.selectById(id);
        int i = roleMapper.deleteById(id);
        roleMenuMapper.delete(new QueryWrapper<RoleMenu>().eq("role_id",id));
        List<Menu> menuList = menuService.list();
        menuList.forEach(item -> {
            Menu menu = menuService.getMenuById(item.getId());
            String[] split = menu.getMeta().getRoles().split(",");
            if (Arrays.asList(split).contains(role.getRoleName())){
                ArrayList<String> list = new ArrayList<>(Arrays.asList(split));
                list.remove(role.getRoleName());
                Meta meta = new Meta();
                meta.setMetaId(menu.getMetaId());
                meta.setRoles(StringUtils.join(list.toArray(),","));
                metaService.updateById(meta);
            }
        });
        return i;
    }
}
